(function ($) {
jQuery.fn.extend({
fd_panel: function(options_in, _callback){
	var options = {
			  id				: null
			, title				: "Panel"
			, canExpand			: true
			, canClose			: false
			, canResize			: false
			, showBorder		: true
			, showHeader		: true
			, width				: null
			, height			: null
			, draggable			: false
			, cssHeaderClass	: "ui-widget-header"
			, cssContentClass	: "ui-widget-content"
			
			//EVENTS
			, onClose			: null
			
		};
	
	options = $.extend(true, options, options_in);
	if (options.id == null)
		options.id = "";
	else
		options.id = $.trim(options.id);
	
	if (options.title == null)
		options.title = "";
	else
		options.title = $.trim(options.title);
	
	if (options.width <= 0) options.width = null;
	if (options.height <= 0) options.height = null;
	
	this.each(function(){
		var $this	= $(this);
		var height	= options.height;
		var width	= options.width;
		var body_content = $this.children();
		
		if (width == null){
			var w = $this.css("width");
			if (parseFloat(w) > 0) width = parseFloat(w);
			if (w.substr(w.length -1) == "%") width = w;
		}
		
		if (height == null){
			var h = $this.css("height");
			if (parseFloat(h) > 0) height = parseFloat(h);
			if (h.substr(h.length - 1) == "%") height = h;
		}
		
		var divs = $this.children("div");
		if (divs.length == 2){
			options.title = $(divs[0]).html();
			body_content = divs[1];
		}
		
		var grp_id = (options.id == "" ? "" : " id = " + options.id);
		
		var grp_ui = $('<div style="display:inline-block;padding-bottom:2px;padding-top:3px;" '+grp_id+' class="fd-panel">');
		
		grp_ui.uniqueId();
		
		var hdr = $('<div style="display:inline-block;width:100%;padding-top:5px;padding-bottom:5px;" class="fd-panel-header">');
		var btn = $("<span class='ui-icon ui-icon-triangle-1-s fd-panel-expand' style='cursor:pointer;float:left'>");
		var title = $("<span style='float:left;padding-left:0px;' class='fd-panel-title'>").html(options.title);
		
		var close_btn = $("<span class='ui-icon ui-icon-close fd-panel-close' style='cursor:pointer;float:right'>");
		
		if (true == options.showHeader)
			hdr.appendTo(grp_ui);
		
		//if (null !== width){
			//grp_ui.css("width", width);
		//} else {
			//grp_ui.css("width", "100%");
		//}
		
		if (options.title != ""){
			if (true == options.canExpand)
				hdr.append(btn);
			
			if (true == options.canClose){
				hdr.append(close_btn);
			}
			
			hdr.append(title);
			hdr.addClass(options.cssHeaderClass);
		}
		
		//if (options.title != "" && options.showBorder == true)
			//grp_ui.addClass("ui-border-all");

		var content = $('<div class="fd-panel-content" style="display:inline-block;">').appendTo(grp_ui);
		content.addClass(options.cssContentClass);
		
		content.css({"height": "100%", "width": "100%"});
		//$this.css("height", "");
		//if (null !== height){
			//content.css("height", height);
		//} 
		
		btn.click(function(){
			if (btn.hasClass("ui-icon-triangle-1-e")){
				btn.removeClass("ui-icon-triangle-1-e");
				btn.addClass("ui-icon-triangle-1-s");
				content.show({effect:"blind"});
				//grp_ui.resize();
			} else {
				btn.addClass("ui-icon-triangle-1-e");
				btn.removeClass("ui-icon-triangle-1-s");
				content.hide({effect:"blind"});
				//grp_ui.resize();
			}
		});
		
		close_btn.click(function(){
			grp_ui.remove();
			if (options.onClose) options.onClose();
		});

		content.append(body_content);
		grp_ui.attr("style", grp_ui.attr("style") +";"+ $this.attr("style"));
		grp_ui.attr("class", grp_ui.attr("class") +" "+ $this.attr("class"));
		
		$this.replaceWith(grp_ui);
		if (true == options.draggable){
			var c = grp_ui.parent();
			grp_ui.draggable({
				  containment		: grp_ui.parent()
				, cursor			: "move"
				, handle			: ".fd-panel-header"
			});
		}
		
		grp_ui.css({
			"z-index"	: "1000"
		});
		
		if (true == options.canResize){
			grp_ui.resizable({
				  alsoResize		: content
				, minHeight			: height
				, minWidth			: width
			});
		}
		
	});
}
});
})(jQuery);
